//
//  ItemSelectTableViewCell.m
//  YJlandlord
//
//  Created by 文彬 on 2018/7/1.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "ItemSelectTableViewCell.h"
#import "ItemSelectCollectionViewCell.h"

@interface ItemSelectTableViewCell()<UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *datas;
@property (assign, nonatomic) NSInteger oneLineCount;
@property (assign, nonatomic) UIFont *titleFont;
@property (assign, nonatomic) NSInteger picWidth;
@property (assign, nonatomic) NSInteger verSpace;

@end

@implementation ItemSelectTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.verSpace = 10;
    self.picWidth = 40;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
        
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.collectionView.collectionViewLayout = layout;
    [self.collectionView registerClass:[ItemSelectCollectionViewCell class] forCellWithReuseIdentifier:@"ItemSelectCollectionViewCell"];
    self.collectionView.backgroundColor = [UIColor clearColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

/**
 设置数据和一行的显示个数
 
 @param datas 数组  单项未字典 @{@"title":@"xxx",@"pic":@"xxx"}
 @param cout 一行显示的个数
 */
- (void)setData:(NSArray*)datas oneLineCount:(NSInteger)cout{
    self.datas  = datas;
    self.oneLineCount  = cout;
    [self.collectionView reloadData];
}
/**
 设置字体
 
 @param font 字体 默认为系统15号
 */
- (void)setFont:(UIFont*)font{
    self.titleFont = font;
//    [self.collectionView reloadData];
}

/**
 图片宽度
 
 @param width 宽度 默认40
 */
- (void)setImageWidth:(NSInteger)width{
    self.picWidth = width;
}

/**
 设置垂直方向距离
 
 @param space 距离 默认10
 */
- (void)setVerticalSpace:(NSInteger)space
{
    self.verSpace = space;
}

#pragma mark UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.datas.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ItemSelectCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemSelectCollectionViewCell" forIndexPath:indexPath];
    NSDictionary *dict = self.datas[indexPath.row];
    cell.botlabel.text = dict[@"title"];
    cell.topImage.image = [UIImage imageNamed:dict[@"pic"]];
    if (self.titleFont!=nil) {
        cell.botlabel.font = self.titleFont;
    }
    
    return  cell;
}

//设置每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.frame.size.width/self.oneLineCount, 80);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(self.verSpace, 0, 0, 0);
//    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return self.verSpace;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.itemClicked!=nil) {
        self.itemClicked(@(indexPath.row));
    }
    
}



@end
